#define  _CRT_SECURE_NO_WARNINGS 1
struct ListNode {
    int val;
    struct ListNode* next;    
};

struct ListNode* detectCycle(struct ListNode* head)
{
    struct ListNode* slow, * fast;
    slow = fast = head;
    while (fast && fast->next)
    {
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast)
        {
            struct ListNode* newhead = slow;
            while (head != newhead)
            {
                head = head->next;
                newhead = newhead->next;
            }
            return newhead;
        }
    }
    return NULL;
}